﻿using System.Collections.Generic;

namespace DevOnServer
{
    public enum Sex
    {
        Male = 0,
        Female
    }

    public class CCharacter
    {
        public int ID;

        public string Name;

        public byte Place;

        public Sex Sex;

        public byte Job;

        public byte Hair;

        public int HairColor;

        public byte Head;

        public int FOR;

        public int END;

        public int INT;

        public int DEX;

        public int Level;

        public int WorldID;

        public CVec3Df Position;

        public short Rotation;

        public int HP;

        public int FP;

        public int MP;

        public long Exp;

        public int SkillPoints;

        public int StatPoints;
    }

    public partial class CDatabase
    {
        public bool loadCharacter(CGamePlayer player, string charname)
        {
            CQueryResult result = m_dbc.result(
                "SELECT * FROM characters WHERE account='{0}' AND name='{1}' LIMIT 1;",
                player.Account.ID, charname);

            if (result == null)
                return false;

            if (!result.advance())
            {
                result.Dispose();
                return false;
            }

            CCharacter character = player.Character = new CCharacter();

            character.ID = result.read<int>("id");
            character.Name = result.read<string>("name");
            character.Place = (byte)result.read<int>("place");
            character.Sex = (Sex)result.read<int>("sex");
            character.Job = (byte)result.read<int>("job");
            character.Hair = (byte)result.read<int>("hair");
            character.HairColor = result.read<int>("hairColor");
            character.Head = (byte)result.read<int>("head");
            character.FOR = result.read<int>("stat_FOR");
            character.END = result.read<int>("stat_END");
            character.DEX = result.read<int>("stat_DEX");
            character.INT = result.read<int>("stat_INT");
            character.Level = result.read<int>("level");
            character.WorldID = result.read<int>("worldid");
            character.Position.X = result.read<float>("pos_X");
            character.Position.Y = result.read<float>("pos_Y");
            character.Position.Z = result.read<float>("pos_Z");
            character.Rotation = (short)result.read<int>("rotation");
            character.HP = (int)result.read<int>("HP");
            character.FP = (int)result.read<int>("FP");
            character.MP = (int)result.read<int>("MP");
            character.Exp = result.read<long>("exp");
            character.SkillPoints = result.read<int>("skillPoints");
            character.StatPoints = result.read<int>("statPoints");

            result.Dispose();

            return true;
        }

        public void saveCharacter(CCharacter character)
        {
            m_dbc.query("UPDATE characters SET worldid='{1}', pos_X='{2}', pos_Y='{3}', pos_Z='{4}', rotation='{5}', HP='{6}', FP='{7}', MP='{8}', exp='{9}', skillPoints='{10}', statPoints='{11}', level='{12}' WHERE id='{0}' LIMIT 1;",
                character.ID, character.WorldID, character.Position.X, character.Position.Y, character.Position.Z, character.Rotation,
                character.HP, character.FP, character.MP, character.Exp, character.SkillPoints, character.StatPoints, character.Level);
        }
    }
}